<?php

require_once 'BaseValidator.class.php';

class StringValidator extends BaseValidator {
	public function __construct($options = array()) {
		$this->options['max_length'] = 255;
		$this->options['min_length'] = 0;
		$this->messages['max_length_exceeded'] = 'В поле должно быть менее %max_length% символов.';
		$this->messages['min_length_not_exceeded'] = 'В поле должно быть более %min_length% символов.';
		
		parent::__construct($options);
	}
	
	public function doClean($value) {
		extract($this->options);
		
		$value = trim((string)$value);
		$current_length = strlen($value);
		if ($current_length > $max_length) {
			throw new ValidatorError($this, 'max_length_exceeded', array(
				'%current_length%' => $current_length,
				'%max_length%' => $max_length
			));
		}
		if ($current_length <= $min_length) {
			throw new ValidatorError($this, 'min_length_not_exceeded', array(
				'%current_length%' => $current_length,
				'%min_length%' => $min_length
			));
		}
		return $value;
	}

}
